package com.itbour.service.material.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.itbour.com.page.JspPage;
import com.itbour.com.page.Page;
import com.itbour.common.util.StringUtils;
import com.itbour.dao.material.ImageDao;
import com.itbour.dto.com.ImageInfoDto;
import com.itbour.dto.goods.ResortDto;
import com.itbour.service.material.ImageService;


/**
*
* copyright   itbour team 2015
* @author     xc
*
*/

public class ImageServiceImpl implements ImageService {

	private ImageDao imageDao;
	
	/**
	 * @param countKey
	 *            查询该表所有记录行的SQL语句ID
	 * @param sqlKey
	 *            需要查询记录的SQL语句ID
	 * @param param
	 *            传递给查询语句的参数
	 * @param pageNo
	 *            需要查询的实际页
	 * @param pageSize
	 *            每页记录行数
	 * @return page对象
	 */
	public Page pageQuery(Map<String,Object> param, Integer pageNo, Integer pageSize) {
		try {
			Page result = null;
			
			// 实际起始位置
			int skipResults = JspPage.getStartOfPage(pageNo, pageSize);
			int totalSize = imageDao.selectImageCnt(param);
			
			while(totalSize >0 && totalSize <= skipResults){
				skipResults= skipResults - pageSize;
				pageNo = pageNo -1;
			}
			
			// 分页检索
			param.put("pageSize", pageSize);
			param.put("skipResults", skipResults);
			List<ImageInfoDto> data = imageDao.selectImageInfo(param);

			result = new JspPage(skipResults, totalSize, pageSize, data);
			return result;
		} finally {
			//clearSelectDataSource(dataSource);
		}
	}

	public List<ImageInfoDto> selectImageInfo(Map<String,Object> param) {
		return imageDao.selectImageInfo(param);
	}
	
	public void deleteImageInfo(String delIds) {
		/*List<Long> idLst = new ArrayList<Long>();
		String[] arrId = null;
		if (StringUtils.isNotEmpty(delIds)) {
			arrId = delIds.split(",");
		}

		for (String strId : arrId) {
			if (StringUtils.isNotEmpty(strId)) {				
				idLst.add(Long.valueOf(strId));
			}
		}
		
		if (idLst != null && idLst.size() > 0) {
			Map<String, Object> param = new HashMap<String, Object>();
			param.put("ids", idLst);
			imageDao.deleteImageInfo(param);
		}*/
		
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("ids", StringUtils.listToString2(StringUtils.stringToList2(delIds)));
		imageDao.deleteImageInfoLogic(param);
	}

	public void addImage(ImageInfoDto imgInfo) {
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("user_id", imgInfo.getUser_id());
		param.put("img_name", imgInfo.getImg_name());
		param.put("url", imgInfo.getUrl());
		param.put("update_by", imgInfo.getUpdate_by());
		imageDao.insertImageInfo(param);
	}

	public ImageDao getImageDao() {
		return imageDao;
	}

	public void setImageDao(ImageDao imageDao) {
		this.imageDao = imageDao;
	}

}
